package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.SiteServiceClient;
import com.hxkj.domain.po.Site;
import com.hxkj.utils.CommonUtils;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.to.PageTo;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ArticleListDirective
 * @Description TODO
 * @Author huangzhaoyong
 * @Date 2018/9/28 11:18
 * @Version 1.0
 */
@Slf4j
@Component
public class OverallSiteDirective extends AbstractDirective {

    @Autowired
    private SiteServiceClient siteServiceClient;


    @Value("${cms.realPath}")
    private String Path;

    /**
     * 输入参数:每页条数
     */
    public static final String COUNT = "count";
    /**
     * 输入参数:开始页数
     */
    public static final String START = "start";
    /**
     * 输入参数: 类型：为null/空 是列表；
     *              为recommend是推荐；
     *              为classify时是分类显示
     */
    public static final String TYPE = "type";

    /**
     * 输出参数：站点list集合
     */
    public static final String SITE_LIST = "siteList";
    /**
     * 输出参数：站点map集合
     */
    public static final String SITE_MAP = "siteMap";


    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        try {
            Integer count = super.getInt(COUNT, params);
            Integer start = super.getInt(START, params);
            String type = super.getString(TYPE, params);

            //登录token
            String userToken = super.getUserToken(env);
            //是否可视化
            boolean noteNotBlank = StringUtils.isNotBlank(super.getNote(env));
            //组装数据
            assemble(env, type, start, count);

            if(null == body){
                log.debug("分类所在模板至少要加一个空格");
            }

            if (noteNotBlank) {
                //可视化
                super.print(body, params, env, userToken, params);
            }else{
                body.render(env.getOut());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 组装数据
     * @param env
     * @param start
     * @param count
     * @throws Exception
     */
    private void assemble(Environment env, String type, Integer start, Integer count) throws Exception {
        //分页
        PageTo pageParam = new PageTo();
        if(null != start){
            pageParam.setPageNum(start);
        }
        if(null != count){
            pageParam.setPageSize(count);
        }
        //参数
        Site site = new Site();
        site.setDelTag((byte) 0);
        site.setSiteStatus((byte) 0);
        if(StringUtils.isNotBlank(type) && type.equals("recommend")){
            //推荐属性
        }
        //查询
        ParameterDataStruct<Site> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(site);
        parameterDataStruct.setPageTo(pageParam);
        List<Site> siteList = siteServiceClient.getSiteInfoList(parameterDataStruct);
        if(StringUtils.isBlank(type) || type.equals("recommend")){
            //输出数据
            env.setVariable(SITE_LIST, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(siteList));
            return;
        }

        if(type.equals("classify")){
            //转换格式
            Map<String, List<Site>> result = CommonUtils.listToMapList(siteList, "parentTypeName");
            env.setVariable(SITE_MAP, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(result));
            return;
        }
    }
}
